Return to start page

Core/String/Struct Tokenizer.j

Code

		
1			library AStructCoreStringTokenizer requires optional ALibraryCoreDebugMisc
2
3 /**
4 * @link http://www.wc3c.net/showthread.php?t=106830
5 * @author ToukoAozaki
6 * @author Tamino Dauth
7 */
8 struct ATokenizer
9 //dynamic members
10 private string m_separators
11 private integer m_position
12 //start members
13 private string m_string
14
15 //! runtextmacro optional A_STRUCT_DEBUG("\"ATokenizer\"")
16
17 //dynamic members
18
19 /**
20 * @param separators Should be list of separating characters.
21 */
22 public method setSeparators takes string separators returns nothing
23 set this.m_separators = separators
24 endmethod
25
26 public method separators takes nothing returns string
27 return this.m_separators
28 endmethod
29
30 public method setPosition takes integer position returns nothing
31 debug if (position < 0) then
32 debug set position = 0
33 debug call this.print("Position is smaller than 0.")
34 debug endif
35 set this.m_position = position
36 endmethod
37
38 public method position takes nothing returns integer
39 return this.m_position
40 endmethod
41
42 //start members
43
44 public method string takes nothing returns string
45 return this.m_string
46 endmethod
47
48 public static method create takes string whichString returns thistype
49 local thistype this = thistype.allocate()
50 //dynamic members
51 set this.m_separators = " \t"
52 set this.m_position = 0
53 //start members
54 set this.m_string = whichString
55 return this
56 endmethod
57
58 public method hasMore takes nothing returns boolean
59 return this.m_position < StringLength(this.m_string)
60 endmethod
61
62 public method next takes nothing returns string
63 local string subString
64 local integer i
65 local integer j
66 local boolean found
67 local string result
68 if (not this.hasMore()) then
69 return null
70 //invalid separator
71 elseif (this.m_separators == null or this.m_separators == "") then
72 set i = StringLength(this.m_string)
73 else
74 set found = false
75 set i = this.m_position
76 loop
77 exitwhen (i >= StringLength(this.m_string))
78 set subString = SubString(this.m_string, i, i + 1)
79 set j = 0
80 loop
81 exitwhen (j >= StringLength(this.m_separators))
82 if (subString == SubString(this.m_separators, j, j + 1)) then
83 set found = true
84 exitwhen (true)
85 endif
86 set j = j + 1
87 endloop
88 exitwhen (found)
89 set i = i + 1
90 endloop
91 endif
92 set result = SubString(this.m_string, this.m_position, i)
93 set this.m_position = i + 1
94 return result
95 endmethod
96
97 public method reset takes nothing returns nothing
98 set this.m_position = 0
99 endmethod
100 endstruct
101
102 endlibrary